package com.weisy.signin.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;

import com.weisy.signin.BuildConfig;
import com.weisy.signin.SignInApp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import androidx.core.content.FileProvider;

/**
 * @author weisy
 * @function
 * @date 2019/3/22 15:14
 */
public class ShareUtils {
    private Context context;

    public ShareUtils(Context context) {
        this.context = context;
    }

    /**
     * 分享网页
     */
    public void shareUrl(String content) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TITLE, "考勤帮");
        intent.putExtra(Intent.EXTRA_SUBJECT, "一个都跑不了");
        intent.putExtra(Intent.EXTRA_TEXT, content);
        Intent chooserIntent = Intent.createChooser(intent, "分享到：");
        context.startActivity(chooserIntent);
    }

    public void sharePic(Bitmap bgimg0) {
        /** * 分享图片 */
        Intent share_intent = new Intent();
        share_intent.setAction(Intent.ACTION_SEND);//设置分享行为
        share_intent.setType("image/*");  //设置分享内容的类型
        share_intent.putExtra(Intent.EXTRA_TITLE, "考勤帮");
        share_intent.putExtra(Intent.EXTRA_SUBJECT, "一个都跑不了");
        share_intent.putExtra(Intent.EXTRA_STREAM, saveBitmap(bgimg0));
        //创建分享的Dialog
        share_intent = Intent.createChooser(share_intent, "分享到");
        context.startActivity(share_intent);
    }

    private static boolean stringCheck(String str) {
        return null != str && !TextUtils.isEmpty(str);
    }

    /**
     * 将图片存到本地
     */
    private Uri saveBitmap(Bitmap bm) {
        try {
            String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/sign/sign" + ".jpg";
            File f = new File(dir);
            if (!f.exists()) {
                f.getParentFile().mkdirs();
                f.createNewFile();
            }
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
            Uri uri;
            if (Build.VERSION.SDK_INT >= 24) {
                uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", f);
            } else {
                uri = Uri.fromFile(f);
            }
            return uri;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
